import pymysql

################
#              #
#  数工装数据    #
#              #
#              #
############## #
mainDb = pymysql.connect(host='localhost', port=23306, user='root', password='Pr0dkcd_', db="kcdoc")
db_cursor = mainDb.cursor()
if __name__ == '__main__':
    gz_plan_sql = """ 
         SELECT id,plan_code_set FROM kc_gz_plan WHERE  plan_code_set is not null and plan_code_set != ''
    """
    execute = db_cursor.execute(gz_plan_sql)
    main_info = db_cursor.fetchall()
    for e in main_info:
        result = e[1].split(', ')
        for code in result:
            get_end_plan = """ 
               select id from hi_kc_production_plan where production_code = '{0}'
               union all
               select id from kc_production_plan_history where production_code ='{1}'
               """.format(code,code)
            db_cursor.execute(get_end_plan)
            fetchall = db_cursor.fetchall()
            if fetchall:
                save_plan_sql = """insert into kc_gz_plan_set(gz_plan_code,plan_id,gz_plan_id) values ('{0}','{1}','{2}');""".format(
                    code, fetchall[0][0], e[0])
                print(save_plan_sql)
            # else:
            #     print("问题计划",code)